
function FileProgress(file, targetID) {
	this.fileProgressID = file.id;
	this.fileProgressName = file.name;
	this.fileStored = false;
	this.fileStoredID = '';
//	this.progressID = '';
//	this.fileUploadCollectionObject = null;
//	this.fileUploadCollectionID = '';
//	this.previewHandlerFile = '';

	this.opacity = 100;
	this.height = 0;
	

	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "javascript:void(0)";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode(file.name));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);

		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.reset();
	}

	this.height = this.fileProgressWrapper.offsetHeight;
	this.setTimer(null);


}

FileProgress.prototype.setTimer = function (timer) {
	this.fileProgressElement["FP_TIMER"] = timer;
};
FileProgress.prototype.getTimer = function (timer) {
	return this.fileProgressElement["FP_TIMER"] || null;
};

FileProgress.prototype.reset = function () {
	this.fileProgressElement.className = "progressContainer";

	this.fileProgressElement.childNodes[2].innerHTML = "&nbsp;";
	this.fileProgressElement.childNodes[2].className = "progressBarStatus";
	
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = "0%";
	
	this.appear();	
};

FileProgress.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";

	this.appear();	
};
FileProgress.prototype.setComplete = function() {
    this.fileProgressElement.className = "progressContainer blue";
    this.fileProgressElement.childNodes[3].className = "progressBarComplete";
    this.fileProgressElement.childNodes[3].style.width = "";

    var oSelf = this;

    //	this.setTimer(setTimeout(function () {
    //		oSelf.disappear();
    //	}, 10000));
};
FileProgress.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	this.setTimer(setTimeout(function () {
		oSelf.disappear();
	}, 5000));
};
FileProgress.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

	var oSelf = this;
	this.setTimer(setTimeout(function () {
		oSelf.disappear();
	}, 2000));
};
FileProgress.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

// Show/Hide the cancel button
FileProgress.prototype.toggleCancel = function(show, swfUploadInstance, flag) {
    this.fileProgressElement.childNodes[0].style.visibility = show ? "visible" : "hidden";
    if (flag == undefined) flag = true;
    if (!show) {
        if (swfUploadInstance) {
            // Begin
            var divOptional = document.createElement("div");
            divOptional.className = "optional";

            var fileSelection = document.createElement("a");
            fileSelection.className = "progressCancel";
            fileSelection.href = "javascript:void(0)";
            fileSelection.appendChild(document.createTextNode(" "));

            var spanSelection = document.createElement("span");
            spanSelection.appendChild(fileSelection);

            var oSelf = this;
            fileSelection.onclick = function() {

                if (flag) swfUploadInstance.customSettings.fileUploadCollectionObject.removeFile(swfUploadInstance.customSettings.progressID + oSelf.fileProgressName);
                else swfUploadInstance.customSettings.fileUploadCollectionObject.removeFile(oSelf.fileProgressName);

                if (swfUploadInstance.customSettings.fileUploadCollectionObject.selectedIndex >= 0) {
                    $('#' + swfUploadInstance.customSettings.progressTarget).find(':radio').each(function(i) {
                        if (this.value == swfUploadInstance.customSettings.fileUploadCollectionObject.selectedValue)
                            this.checked = true;
                    });
                }

                if (document.getElementById(swfUploadInstance.customSettings.fileUploadCollectionID)) {
                    document.getElementById(swfUploadInstance.customSettings.fileUploadCollectionID).value = swfUploadInstance.customSettings.fileUploadCollectionObject.toString();
                }

                if (document.getElementById(swfUploadInstance.customSettings.defaultFileUploadedID))
                    document.getElementById(swfUploadInstance.customSettings.defaultFileUploadedID).value = swfUploadInstance.customSettings.fileUploadCollectionObject.selectedIndex;

                oSelf.setTimer(setTimeout(function() {
                    oSelf.disappear();
                }, 200));
            };
            //fileSelection.name = fileSelection.id = "chk" + this.fileProgressID;
            //fileSelection.value = this.fileProgressName;
            //fileSelection.checked = true;

            var defaultSelection = document.createElement("input");
            defaultSelection.type = "radio";
            defaultSelection.name = swfUploadInstance.customSettings.defaultFileUploadedID;
            defaultSelection.id = "rdo" + this.fileProgressID;
            if (flag) defaultSelection.value = swfUploadInstance.customSettings.progressID + this.fileProgressName;
            else defaultSelection.value = this.fileProgressName;

            if (swfUploadInstance.customSettings.fileUploadCollectionObject.selectedIndex >= 0 && defaultSelection.value == swfUploadInstance.customSettings.fileUploadCollectionObject.selectedValue) {
                defaultSelection.checked = true;
            }

            defaultSelection.onclick = function() {
                swfUploadInstance.customSettings.fileUploadCollectionObject.setSelected(this.value);

                if (document.getElementById(swfUploadInstance.customSettings.selectedIndexID))
                    document.getElementById(swfUploadInstance.customSettings.selectedIndexID).value = swfUploadInstance.customSettings.fileUploadCollectionObject.selectedIndex;
            };
            //defaultSelection.checked = true;

            divOptional.appendChild(defaultSelection);
            divOptional.appendChild(spanSelection);

            this.fileProgressElement.replaceChild(divOptional, this.fileProgressElement.firstChild);

            //var imageType = /(\.jpg|\.gif|\.png|\.bmp)$/gi;

            var fileProgressName = this.fileProgressName.toLowerCase();

            if (fileProgressName.indexOf('.jpg') > 0
                || fileProgressName.indexOf('.gif') > 0
                || fileProgressName.indexOf('.png') > 0
                || fileProgressName.indexOf('.bmp') > 0) {
                // Hien thumbnail
                var divThumb = document.createElement("div");
                divThumb.className = "progressThumbnail";

                var thumbImage = document.createElement("img");
                if (this.fileStored != undefined && this.fileStored == true) {
                    thumbImage.src = swfUploadInstance.customSettings.previewHandlerFile + '?fid=' + this.fileStoredID + '&stored=true';
                }
                else {
                    thumbImage.src = swfUploadInstance.customSettings.previewHandlerFile + "?fid=" + encodeURIComponent(this.fileProgressName) + '&path=' + encodeURI(swfUploadInstance.customSettings.saveToPath);
                    if (flag) thumbImage.src += "&sid=" + swfUploadInstance.customSettings.progressID
                }

                var divElement = document.createElement("div");
                divElement.appendChild(this.fileProgressElement.childNodes[1]);
                divElement.appendChild(this.fileProgressElement.childNodes[1]);
                divElement.appendChild(this.fileProgressElement.childNodes[1]);

                divThumb.appendChild(thumbImage);
                divThumb.appendChild(divElement);

                this.fileProgressElement.appendChild(divThumb);
            }
        }
        // End
    }
    if (swfUploadInstance && show) {
        var fileID = this.fileProgressID;
        var oSelf = this;
        this.fileProgressElement.childNodes[0].onclick = function() {

            if (flag) swfUploadInstance.customSettings.fileUploadCollectionObject.removeFile(swfUploadInstance.customSettings.progressID + oSelf.fileProgressName);
            else swfUploadInstance.customSettings.fileUploadCollectionObject.removeFile(oSelf.fileProgressName);

            if (document.getElementById(swfUploadInstance.customSettings.fileUploadCollectionID)) {
                document.getElementById(swfUploadInstance.customSettings.fileUploadCollectionID).value = swfUploadInstance.customSettings.fileUploadCollectionObject.toString();
            }

            swfUploadInstance.cancelUpload(fileID);

            oSelf.setTimer(setTimeout(function() {
                oSelf.disappear();
            }, 2000));
            return false;
        };
    }
};

FileProgress.prototype.appear = function () {
	if (this.getTimer() !== null) {
		clearTimeout(this.getTimer());
		this.setTimer(null);
	}
	
	if (this.fileProgressWrapper.filters) {
		try {
			this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 100;
		} catch (e) {
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
		}
	} else {
		this.fileProgressWrapper.style.opacity = 1;
	}
		
	this.fileProgressWrapper.style.height = "";
	
	this.height = this.fileProgressWrapper.offsetHeight;
	this.opacity = 100;
	this.fileProgressWrapper.style.display = "";
	
};

// Fades out and clips away the FileProgress box.
FileProgress.prototype.disappear = function () {

	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 30;	// 15 fps

	if (this.opacity > 0) {
		this.opacity -= reduceOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}

	if (this.height > 0) {
		this.height -= reduceHeightBy;
		if (this.height < 0) {
			this.height = 0;
		}

		this.fileProgressWrapper.style.height = this.height + "px";
	}

	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		this.setTimer(setTimeout(function () {
			oSelf.disappear();
		}, rate));
	} else {
		this.fileProgressWrapper.style.display = "none";
		this.setTimer(null);
	}
};